<!--
/**
 * Opens a new windows by specified width and height.
 */
var popWindow = null;

function openWindow(url, width, height, scroll, resize) {
    if (popWindow == null || popWindow.closed) {
        if (scroll && !resize) {
            popWindow = window.open(url, null, 'height=' + height + 'px,width=' + width + 'px,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no');
        }
        else if(resize) {
            popWindow = window.open(url, null, 'height=' + height + 'px,width=' + width + 'px,left=' + (screen.availWidth-width)/2 + ',top=' + (screen.availHeight-height)/2 + ',status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no');
        }
        else {
            popWindow = window.open(url, null, 'height=' + height + 'px,width=' + width + 'px,status=no,toolbar=no,menubar=no,location=no,resizable=no');
        }
    }
    else {
        popWindow.focus();
        if (!!document.all) {
            popWindow.resizeTo(width, height + 80);
        }
        else {
            popWindow.resizeTo(width, height + 55);
        }
        popWindow.location = url;
    }
}

function checkAll(nameItem, itemAll)
{
  var a = document.getElementsByName(nameItem);
  var n = a.length;
  for (var i=0; i<n; i++) {
    a[i].checked = itemAll.checked;
  }
}

function checkItem(nameAll, item)
{
  var e = item;
  var all = document.getElementById(nameAll);
  if (e.checked)
  {
    var a = document.getElementsByName(e.name);
    all.checked = true;
    for (var i=0; i<a.length; i++)
    {
      if (!a[i].checked){ all.checked = false; break;}
    }
  }
  else {
    all.checked = false;
  }
}


/**
 * Checks state status for USA.
 */
function validateStates(countrySelect) {
    if ('US' == countrySelect.value) {
        document.getElementById("selectState").disabled = false;
        if (null != document.getElementById("ssn")) {
            document.getElementById("ssn").disabled = false;
        }
        if (null != document.getElementById("taxList")) {
            document.getElementById("taxList").disabled = false;
        }
        if (null != document.getElementById("stateStar")) {
            document.getElementById("stateStar").style.visibility = 'visible';
        }
        document.getElementById("usaZipCode").style.visibility = 'visible';
        if (null != document.getElementById("taxClassification")) {
            document.getElementById("taxClassification").style.visibility = 'visible';
        }
        if (null != document.getElementById("ssnTaxID")) {
            document.getElementById("ssnTaxID").style.visibility = 'visible';
        }
        document.getElementById("usaZipCodeCheck").className = 'zipcode';
        if (null != document.getElementById("ssnEmpty")) {
            document.getElementById("ssnEmpty").className = 'ssn';
        }
    }
    else {
        document.getElementById("selectState").disabled = true;
        if (null != document.getElementById("ssn")) {
            document.getElementById("ssn").disabled = true;
        }
        if (null != document.getElementById("taxList")) {
            document.getElementById("taxList").disabled = true;
        }
        if (null != document.getElementById("stateStar")) {
            document.getElementById("stateStar").style.visibility = 'hidden';
        }
        document.getElementById("usaZipCode").style.visibility = 'hidden';
        if (null != document.getElementById("taxClassification")) {
            document.getElementById("taxClassification").style.visibility = 'hidden';
        }
        if (null != document.getElementById("ssnTaxID")) {
            document.getElementById("ssnTaxID").style.visibility = 'hidden';
        }
        document.getElementById("usaZipCodeCheck").className = 'nonecss';
        if (null != document.getElementById("ssnEmpty")) {
            document.getElementById("ssnEmpty").className = 'nonecss';
        }
    }
}

/**
 * Shows the limit message on input field.
 */
function showLimitMessage(inputObj, spanObj, maxLimit) {
    var strLength = 0;
    if (inputObj.value != null) {
        strLength = inputObj.value.length;
    }
    if (strLength <= maxLimit) {
        spanObj.innerHTML = (maxLimit - strLength);
    }
    else {
        spanObj.innerHTML = "<span style=\"color:red\">Too many characters!</span>";
    }
}

/**
 * Judege whether array has duplicate element.
 */
function hasDuplicateElement(originalArray) {
    var newArray = new Object();
    for (var i = 0, j = 0; i < originalArray.length; i++) {
        if ('undefined' == typeof newArray[originalArray[i]]) {
            newArray[originalArray[i]] = j++;
        }
        else {
            return true;
        }
    }

    return false;
}

/**
 * Cuts the content in an input field that limits the content length.
 */
function cutContent(inputObj, maxLimit) {
    var contentText = inputObj.value;
    if (contentText.length > maxLimit) {
        inputObj.value = contentText.substring(0, maxLimit);
    }
}
/**
 * Escapes the special html characters.
 */
function htmlSpecialChars(code) {
    code = code.replace(/&/g, '&amp;');
    code = code.replace(/"/g, '&quot;');
    code = code.replace(/'/g, '&#039;');
    code = code.replace(/</g, '&lt;');
    code = code.replace(/>/g, '&gt;');
    return code;
}

/**
 * Adds ellipsis for ad headline and description.
 */
function stripAllContents(elements) {
    var striped = false;

    for (var i in elements) {
        striped |= stripContent(elements[i]);
    }
    return striped;
}

/**
 * Adds ellipsis for broker link.
 */
function stripBrokerLink(preview) {
    var width = preview.totalWidth;
    var element = preview.brokerLinkSpan;

    var originalText = element.innerHTML;
    element.innerHTML = 'jJ';
    var lineHeight = element.scrollHeight * 1.5;
    element.innerHTML = originalText;
    if ((element.scrollWidth <= width) && (element.scrollHeight <= lineHeight)) {
        return false;
    }

    var lastText = "";
    var currentText = "";
    for(var i=0; i < originalText.length; i++) {
        currentText += originalText.charAt(i);
        element.innerHTML=currentText;
        if ((element.scrollWidth > width) || (element.scrollHeight > lineHeight)) {
            lastText = lastText.substr(0, lastText.length - 4) + "...";
            element.innerHTML=lastText;
            return true;
        }
        lastText=currentText;
    }
}
/**
 * Adds ellipsis for ad headline and description.
 */
function stripContent(element) {
    var nodes = element.childNodes;
    var div_nodes = new Array();
    var idx = 0;
    var striped = false;
    for (var i=0;i< nodes.length;i++) {
         if (1 == nodes[i].nodeType) {
            div_nodes[idx++] = nodes[i];
         }
    }
    var titleElement = div_nodes[0];
    var descriptionElement = div_nodes[1];
    var width=parseInt(element.style.width);
    var height=parseInt(element.style.height);
    if ((titleElement.parentNode.scrollWidth > width) || (descriptionElement.parentNode.scrollWidth > width) || (titleElement.offsetHeight + descriptionElement.offsetHeight > height)) {
        var backDescription = descriptionElement.innerHTML;
        descriptionElement.innerHTML = '';
        var striped = stripTitle(titleElement, width, height);
        if (!striped) {
            descriptionElement.innerHTML = backDescription;
            var backTitle = titleElement.innerHTML;
            var titleHeight = titleElement.offsetHeight;
            striped |= stripDescription(descriptionElement, width, height - titleHeight);
            //When description can't display, add ellipsis to the end of title
            if (("..." == descriptionElement.innerHTML) && ("..." != backDescription)) {
                descriptionElement.innerHTML = '';
                descriptionElement.style.display = 'none';//Set description height to 0 for IE.
                for(var j=0;j<3;j++) {
                    backTitle += ".";
                    titleElement.innerHTML = backTitle;
                    //check whether after add dot to the end of title cause overflow
                    if (titleElement.parentNode.scrollWidth > width || titleElement.parentNode.scrollHeight > height) {
                        for (var k = 3; 0 < k; --k) {
                            if (backTitle.search(/((&lt;)|(&gt;)|(&quot;)|(&amp;)|(&#039;))$/) >= 0) {
                                backTitle = backTitle.replace(/((&lt;)|(&gt;)|(&quot;)|(&amp;)|(&#039;))$/, "");
                            }
                            else {
                                backTitle = backTitle.substr(0, (backTitle.length-1));
                            }
                        }
                        backTitle = backTitle + '...';
                        break;
                    }
                }
                titleElement.innerHTML =  backTitle;
            }
        }
    }

    return striped;

}

/**
 * Adds ellipsis for ad headline.
 */
function stripTitle(element, width, height) {
    if ((element.offsetHeight <= height) && (element.parentNode.scrollWidth <= width)) {
        return false;
    }
    var originalText = element.innerHTML;
    var lastText = "";
    var currentText = "";
    var nextText = "";
    var position = "";
    for(var i=0;i<originalText.length;i++) {
        nextText = originalText.charAt(i);
        if ('&' == originalText.charAt(i)) {
            do {
                nextText += originalText.charAt(++i);
            } while (';' != originalText.charAt(i));
        }
        currentText += nextText;
        element.innerHTML = currentText;
        if (element.parentNode.scrollWidth > width) {
            currentText = lastText + "<br>" + nextText;
            element.innerHTML = currentText;
        }
        if(element.offsetHeight>height) {
            for (position = lastText.length; 0 < position; position--) {
                if (' ' == lastText.charAt(position-1)) {
                    break;
                }
            }
            if (0 == position) {
                position = lastText.length;
                for (var j = 3; 0 < j; j--, position--) {
                    if (lastText.search(/((&lt;)|(&gt;)|(&quot;)|(&amp;)|(&#039;))$/) >= 0) {
                        lastText = lastText.replace(/((&lt;)|(&gt;)|(&quot;)|(&amp;)|(&#039;))$/, "");
                    }
                    else {
                        lastText = lastText.substr(0, lastText.length-1);
                    }
                }
            }
            lastText = lastText + '...';
            element.innerHTML = lastText;

            return true;
        }
        lastText=currentText;
    }

    return false;
}

/**
 * Adds ellipsis for ad description.
 */
function stripDescription(element,width,height) {
    if (element.offsetHeight <= height && element.parentNode.scrollWidth <= width) {
        return false;
    }
    var originalText=element.innerHTML;
    var lastText="";
    var currentText="";
    var nextText = "";
    var position = "";
    for(var i=0;i<originalText.length;i++) {
        nextText = originalText.charAt(i);
        if ('&' == originalText.charAt(i)) {
            do {
                nextText += originalText.charAt(++i);
            } while (';' != originalText.charAt(i));
        }

        currentText += nextText;
        element.innerHTML=currentText;
        if (element.parentNode.scrollWidth>width) {
            currentText=lastText + "<br>" + nextText;
            element.innerHTML=currentText;
        }
        if (element.offsetHeight>height) {
            if (lastText.length == 0) {
                element.innerHTML = "...";
            }
            else {          
                if (3 < lastText.length) {
                    for (position = lastText.length; 0 < position; position--) {
                        if (' ' == lastText.charAt(position - 1)) {
                            break;
                        }
                    }
                    if (0 == position) {
                        position = lastText.length;
                        for (var j = 3; 0 < j; j--, position--) {
                            if (';' == lastText.charAt(position - 1)) {
                                while ('&' != lastText.charAt(position - 1)) {
                                    position--;
                                }
                            }
                        }
                    }
                    lastText = lastText.substr(0, position) + '...';
                    element.innerHTML = lastText;
                    
                }
            }
            return true;
        }
        lastText=currentText;
    }
    return false;
}

/**
 * Highlight all the selected rows in a page.
 */
function highlightSelectedRows(checkboxsName, buttonsName) {
    buttons = buttonsName ? document.getElementsByName(buttonsName) : new Array();
    if (checkboxes = document.getElementsByName(checkboxsName)) {
        for (var i = 0; i < checkboxes.length; ++i) {
            highlightOneRow(checkboxes[i], buttons[i]);
        }
    }
}

/**
 * Highlight the row of the checkbox
 */
function highlightOneRow(checkBox, button) {
    currentNode = checkBox.parentNode;
    while (button && ('TD' != button.tagName)) {
        button = button.parentNode;
    }
    while ('TR' != currentNode.tagName) {
        currentNode = currentNode.parentNode;
    }
    if (checkBox.checked && !checkBox.disabled) {
        if (null == checkBox.originalColor) {
            checkBox.originalColor = currentNode.style.backgroundColor;
        }
        currentNode.style.backgroundColor = '#F8F685';
        if (button) {
            button.style.backgroundColor = checkBox.originalColor;
        }
    }
    else {
        currentNode.style.backgroundColor = checkBox.originalColor;
    }
}

/**
 * Get the browser infomation.
 */
function browserInfo(){
    var Browser_Name=navigator.appName;
    var Browser_Version=parseFloat(navigator.appVersion);
    var Browser_Agent=navigator.userAgent;
    var Actual_Version,Actual_Name;

    var is_IE=(Browser_Name=="Microsoft Internet Explorer");
    var is_NN=(Browser_Name=="Netscape");

    if(is_NN){
        //upper 5.0 need to be process,lower 5.0 return directly
        if(Browser_Version>=5.0){
            var Split_Sign=Browser_Agent.lastIndexOf("/");
            var Version=Browser_Agent.indexOf(" ",Split_Sign);
            var Bname=Browser_Agent.lastIndexOf(" ",Split_Sign);

            Actual_Version=Browser_Agent.substring(Split_Sign+1,Version);
            Actual_Name=Browser_Agent.substring(Bname+1,Split_Sign);
        }
        else{
            Actual_Version=Browser_Version;
            Actual_Name=Browser_Name;
        }
    }
    else if(is_IE){
        var Version_Start=Browser_Agent.indexOf("MSIE");
        var Version_End=Browser_Agent.indexOf(";",Version_Start);
        Actual_Version=Browser_Agent.substring(Version_Start+5,Version_End)
        Actual_Name=Browser_Name;

        if(Browser_Agent.indexOf("Maxthon")!=-1){
            Actual_Name+="(Maxthon)";
        }
        else if(Browser_Agent.indexOf("Opera")!=-1){
            Actual_Name="Opera";
            var tempstart=Browser_Agent.indexOf("Opera");
            var tempend=Browser_Agent.length;
            Actual_Version=Browser_Agent.substring(tempstart+6,tempend)
        }
    }
    else{
        Actual_Name="Unknown Navigator"
        Actual_Version="Unknown Version"
    }

    navigator.Actual_Name=Actual_Name;
    navigator.Actual_Version=Actual_Version;
}

/**
 * Check whether the browser is IE7.
 */
function isIE7() {
    var result = false;

    browserInfo();
    if ('Microsoft Internet Explorer' == navigator.Actual_Name && 7 == navigator.Actual_Version) {
        result = true;
    }
    return result;
}

/**
 * Control the performance in IE7.
 */
function controlIE7Performance() {
    var controlDiv = document.getElementById("IE7Control");
    var control = document.getElementById('control');
    //in some page there is a inner div controling the main width in IE7
    var innerControlDiv = document.getElementById('innerControl');
    var mainWidth = document.getElementById('mainWidth');
    //get the accurate offsetLeft of the element
    if (controlDiv) {
        controlDiv.align = 'center';
    }
    var eLeft = 0;
    var element = (innerControlDiv && controlDiv) ? innerControlDiv : control;
    while(element) {
        eLeft += element.offsetLeft;
        element = element.offsetParent;
    }
    //Get the width of the "mainPage" table to control the alignment of div "IE7Control"(for fixing bug 4824)
    var mainPage = document.getElementById('mainPage');

    if (innerControlDiv && controlDiv) {
        controlDiv.align = (eLeft + innerControlDiv.scrollWidth > mainWidth.width) ? 'left' : 'center';
    }
    else if (controlDiv){
        if (mainWidth) {
            controlDiv.align = (eLeft + control.scrollWidth > mainWidth.width) ? 'left' : 'center';
        }
        else {
            controlDiv.align = (controlDiv.scrollWidth > parseInt(mainPage.style.width)) ? 'left' : 'center';
        }
    }
}

/**
 * Fade all the unselected rows in a page.
 */
function fadeUnselectedRows(checkboxsName, isfade, buttonsName) {
    if (checkboxes = document.getElementsByName(checkboxsName)) {
        for (var i = 0; i < checkboxes.length; ++i) {
            checkBox = checkboxes[i];
            if (!checkBox.checked) {
                var currentNode = checkBox.parentNode;
                if (null == checkBox.originalColor) {
                    checkBox.originalColor = currentNode.style.backgroundColor;
                }
                if (isfade) {
                    var node = currentNode.parentNode;
                    node.style.color = '#A9A9A9';
                    var len = node.cells.length;
                    for (var j = 0; j < len; j++) {
                        var cell = node.cells[j];
                        if (cell.firstChild) {
                            var child = cell.firstChild;
                            while (child) {
                                if (3 != child.nodeType) {
                                    child.style.color = '#A9A9A9';
                                }
                                child = child.nextSibling;
                            }
                        }
                    }
                }
                else {
                    var node = currentNode.parentNode;
                    node.style.color = '#5E5E5E';
                    var len = node.cells.length;
                    for (var j = 0; j < len; j++) {
                        var cell = node.cells[j];
                        if (cell.firstChild) {
                            var child = cell.firstChild;
                            while (child) {
                                if (3 != child.nodeType) {
                                    child.style.color = '#5E5E5E';
                                }
                                child = child.nextSibling;
                            }
                        }
                    }
                }
            }
        }
    }
}

/**
 * Truncates the string with specified string(such as '...') by specified length.
 */
function truncateString(string, len) {
    if (string.length > len) {
        return string.substring(0, len) + '...';
    }
    else {
        return string;
    }
}

/**
 * Shiled the link in the srcElement
 * @param {Object} srcElement
 */
function shieldLink(srcElement) {
    var position = PopUpLayer.getCoordinate(srcElement);
    if (document.getElementById('shellImage')) {//if the image is exist, do not need to draw again
        return null;
    }
    var outer = document.createElement('img');
    outer.id = 'shellImage';
    outer.style.width = srcElement.offsetWidth + 'px';
    outer.style.height = srcElement.offsetHeight + 'px';
    outer.style.top = position.top - srcElement.offsetHeight + 'px';
    outer.style.left = position.left - srcElement.offsetWidth + 'px';
    outer.style.position = 'absolute';
    if (!!document.all) {
        outer.style.filter = 'alpha(opacity=0)';
    }
    else {
        outer.style.opacity = 0;
    }
    srcElement.parentNode.appendChild(outer);
}

/**
 * Add check for change action on all links in the page
 * @param {String} leaveMessage
 */
function addLinksCheckChange(leaveMessage) {
    var links = document.getElementsByTagName("A");
    for (var i = 0; i < links.length; i++) {
        if (links[i].href.indexOf("javascript") == -1) {
            links[i].onclick = addCheckChange(links[i].onclick, leaveMessage);
        }
    }
}

/**
 * Add check for change action to the element
 * @param {Function} originalFunction
 * @param {String} leaveMeesgae
 */
function addCheckChange(originalFunction, leaveMessage) {
    return function(event) {
        if ('undefined' != typeof(isChanged)) {//It's a global variable in the page
            if (('undefined' != typeof(thisField)) && thisField) {
                thisField.blur();
                if (!result) {
                    return false;
                }
            }
            if (isChanged && confirm(leaveMessage)) {
                return false;
            }
        }
        else if ('undefined' != typeof(changed)) {//It's a global variable in the page
            if (changed && confirm(leaveMessage)) {
                return false;
            }
        }
        if ("function" == typeof originalFunction) {
            return originalFunction(event);
        }
    };
}

/**
 * Check if the content has any change, if changed then popup a confirm
 * @param {String} leaveMeesgae
 */
function checkChange(isChanged, leaveMessage) {
    return isChanged ? !confirm(leaveMessage) : true;
}

/**
 * This function is used to work with onClickPalette() function for browser compatibility.
 * @param {Function} originalFunction
 */
function commonPreview(originalFunction) {
    if ("function" == typeof(originalFunction)) {
        if (!document.all && "undefined" != typeof(onchangeFlag)) { //onchangeFlag is a global variable in the page
            onchangeFlag = true; //set true, so that color palette won't display if onchange event is trigged in FF browser
            originalFunction();
            onchangeFlag = false;
        }
        else {
            originalFunction();
        }
    }
}

/**
 * Round price to the Nth digit from the left. 
 * @param float price
 * @param int precision
 */
function roundPrice(price, precision) {
    var result = 0;

    price = parseFloat(price).toFixed(precision);
    var temp_price = price.split('.');

    var len = Math.abs(temp_price[0]).toString().length;
    var decimal = precision - len;

	if (0 < decimal) {
        decimal = (decimal > 2) ? 2 : decimal;
        result = Math.floor(price * Math.pow(10, decimal) + 0.50000000001) / Math.pow(10, decimal);
        result = result.toFixed(2);
    }
    else {
        result = Math.round(price * Math.pow(10, decimal)) / Math.pow(10, decimal);
        result = result.toFixed(2);
    }
	
    return result;
}